const multer = require('multer');
const svgCaptcha = require('svg-captcha');
const router = require('express').Router();
const getRandomHexColor = () => {
	let letters = '0123456789ABCDEF';
	let hexArrs = Array.from({ length: 6 }).map(() => {
		return letters[Math.floor(Math.random() * 16)];
	});
	return `#${hexArrs.join('')}`;
}
const upload = multer({
	storage: multer.diskStorage({
		destination: (req, file, cb) => cb(null, '../uploads'),
		filename: function(req, file, cb) {
			let str = file.originalname.split('.');
			cb(null, Date.now() + '.' + str[1]);
		}
	})
});

/* GET home page. */
router.get('/', function(req, res, next) {
	console.log(123123);
	res.json({ message: 'Welcome to the API' });
});

router.get('/login', function(req, res, next) {
	console.log(123123);
	res.json({ message: 'Welcome to the API' });
});

router.get('/register', function(req, res, next) {
	console.log(123123);
	res.json({ message: 'Welcome to the API' });
});

router.get('/forgetpass', function(req, res, next) {
	console.log(123123);
	res.json({ message: 'Welcome to the API' });
});

router.post('/upload', upload.array("file", 20), function(req, res, next) {

});

router.get('/captcha', function(req, res, next) {
	let randomColor = getRandomHexColor();
	let captcha = svgCaptcha.create({
		background: '#f0f0f0',
		ignoreChars: '0o1i', // 验证码字符中排除 0o1i
		inverse: true, // 翻转颜色 
		fontSize: 48, // 字体大小 
		noise: 4, // 噪声线条数 
		size: 4 // 验证码长度
	});
	// console.log(captcha);
	/* res.json({ code: 200, data: captcha.data });
	// 保存到session,忽略大小写 
	req.session.captcha = captcha.text.toLowerCase();
	res.cookie('captcha', req.session.captcha);
	res.type('svg'); */
	res.send(String(captcha.data));
});

module.exports = router;